package nl.tudelft.jpacman.npc.ghost;

import nl.tudelft.jpacman.board.Direction;
import nl.tudelft.jpacman.sprite.Sprite;
import nl.tudelft.jpacman.sprite.PacManSprites;
import java.util.Map;
import java.util.EnumMap;

/**
 * 工厂类，用于创建各种幽灵NPC。
 */
public class GhostFactory {

    private final Map<Direction, Sprite> blinkySprites;
    private final Map<Direction, Sprite> pinkySprites;
    private final Map<Direction, Sprite> inkySprites;
    private final Map<Direction, Sprite> clydeSprites;

    /**
     * 创建新的幽灵工厂。
     * @param sprites 用于幽灵的精灵资源
     */
    public GhostFactory(PacManSprites sprites) {
        // 验证sprites参数不为null
        if (sprites == null) {
            throw new IllegalArgumentException("Sprites cannot be null");
        }

        // 初始化各幽灵的精灵图
        this.blinkySprites = createDirectionalSprites(sprites.getGhostSprite(GhostColor.RED));
        this.pinkySprites = createDirectionalSprites(sprites.getGhostSprite(GhostColor.PINK));
        this.inkySprites = createDirectionalSprites(sprites.getGhostSprite(GhostColor.CYAN));
        this.clydeSprites = createDirectionalSprites(sprites.getGhostSprite(GhostColor.ORANGE));
    }

    /**
     * 为幽灵创建包含所有方向的精灵映射。
     * @param sprite 基础精灵
     * @return 包含所有方向的精灵映射
     */
    private Map<Direction, Sprite> createDirectionalSprites(Sprite sprite) {
        Map<Direction, Sprite> spriteMap = new EnumMap<>(Direction.class);
        for (Direction direction : Direction.values()) {
            spriteMap.put(direction, sprite);
        }
        return spriteMap;
    }

    public Blinky createBlinky() {
        return new Blinky(new EnumMap<>(blinkySprites));
    }

    public Pinky createPinky() {
        return new Pinky(new EnumMap<>(pinkySprites));
    }

    public Inky createInky() {
        return new Inky(new EnumMap<>(inkySprites));
    }

    public Clyde createClyde() {
        return new Clyde(new EnumMap<>(clydeSprites));
    }
}
